home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Tool Chest / Development Kits / MPW etc. / MPW-GM / MPW / Examples / AExamples / MemorySrc.a < prev    next >
Encoding:
Text File  |  1996-04-03  |  7.8 KB  |  274 lines  |  [TEXT/MPS ]

  1. ***************************************************************************
  2. ****    
  3. ****    MEMORY DESK ACCESSORY - A sample DA written in MPW 68000 Assembly
  4. ****    
  5. ****    Copyright Apple Computer, Inc. 1985-1987, 1993
  6. ****    All rights reserved.
  7. ****
  8. ***************************************************************************
  9.  
  10.         STRING    PASCAL
  11.  
  12.         MAIN
  13.  
  14.         INCLUDE    'Events.a'    
  15.         INCLUDE 'Devices.a'
  16.         INCLUDE 'Files.a'
  17.         INCLUDE 'Fonts.a'
  18.         INCLUDE 'LowMemEqu.a'
  19.         INCLUDE 'Memory.a'
  20.         INCLUDE    'Traps.a'
  21.         INCLUDE 'Quickdraw.a'
  22.         INCLUDE    'ToolUtils.a'
  23.         INCLUDE    'Windows.a'
  24.  
  25. ; Desk accessories (drivers) cannot use global variables in the normal sense.
  26. ; Usually, a handle is allocated and stuffed into dCtlStorage and global
  27. ; variables are stored in this handle.  However, in this example, the globals
  28. ; are allocated at the end of the desk accessory's window record.  Since the
  29. ; window record is always nonrelocatable storage, the variables will never move.
  30. ; This record structure below defines the layout of our "global variables."
  31.  
  32. GlobalVars    RECORD    WindowRecord.sizeof        ; Put variables at end of window rec
  33. aString     DS.B    28                        ; vol names must be < 28 char
  34. aNumStr     DS.B    10                        ; sufficient for 10 GB of space
  35. GlobalSize    EQU        *-GlobalVars            ; size of my globals
  36.             ENDR
  37.  
  38.     WITH GlobalVars
  39.  
  40. aPBPtr    EQU     D7
  41.  
  42.  
  43. **************************** DESK ACCESSORY ENTRY **************************
  44.         
  45. DAEntry                                         ; See Device Manager IM:2
  46.     DC.B    (1<<dCtlEnable) + (1<<dNeedTime)    ; periodic,control flags set
  47.     DC.B    0                                    ; Lower byte is unused
  48.     DC.W    5*60                                ; 5 sec periodic update
  49.     DC.W    (1<<updateEvt)                        ; Handle only update events
  50.     DC.W    0                                    ; No menu for this accessory
  51.  
  52.     DC.W    DAOpen-DAEntry                        ; Open routine
  53.     DC.W    DADone-DAEntry                        ; Prime - unused
  54.     DC.W    DACtl-DAEntry                        ; Control
  55.     DC.W    DADone-DAEntry                        ; Status - unused
  56.     DC.W    DAClose-DAEntry                     ; Close
  57.  
  58. DATitle
  59.     DC.B    'Free Mem (#Bytes)'                 ; DA Name (& Window Title)
  60.     ALIGN    2                                    ; Word align
  61.  
  62.  
  63. ************************ DESK ACCESSORY OPEN ROUTINE ***********************
  64.  
  65. DAOpen
  66.     MOVEM.L     A1-A4,-(SP)                     ; preserve A1-A4
  67.     MOVE.L        A1,A4                            ; MOVE DCE pointer to a reg
  68.     
  69.     SUBQ.L        #4,SP                            ; FUNCTION = GrafPtr
  70.     MOVE.L        SP,-(SP)                        ; push a pointer to it
  71.     _GetPort                                    ; push it on top of stack
  72.     TST.L        DCtlEntry.dCtlWindow(A4)        ; do we have a window?
  73.     BNE.S        StdReturn                        ; If so, return, Else…
  74.  
  75. ******************************* NEW WINDOW ROUTINE *************************
  76.     WITH        WindowRecord
  77.     
  78.     MOVE.L        #sizeof+GlobalSize,D0
  79.     _NewPtr                                        ; allocate space for record
  80.     SUBQ        #4,SP                            ; FUNCTION = WindowRef
  81.     MOVE.L        A0,-(SP)                        ; address of storage
  82.     PEA         theWindow                        ; boundsRect
  83.     PEA         DATitle                         ; title
  84.     CLR.W        -(SP)                            ; visible flag FALSE
  85.     MOVE.W        #noGrowDocProc,-(SP)            ; window proc
  86.     MOVE.L        #-1,-(SP)                        ; window in front
  87.     MOVE.B        #1,-(SP)                        ; goAway box TRUE
  88.     CLR.L        -(SP)                            ; refCon is 0
  89.     _NewWindow
  90.     MOVE.L        (SP)+,A0
  91.     MOVE.L        A0,DCtlEntry.DCtlWindow(A4)                ; save windowPtr
  92.     MOVE.W        DCtlEntry.DCtlRefNum(A4),windowKind(A0)    ; system window
  93.     _MaxMem
  94.     
  95. StdReturn
  96.     _SetPort                                    ; old port on stack
  97.     MOVEM.L     (SP)+,A1-A4                     ; restore regs
  98.     ENDWITH
  99.  
  100.  
  101. ************************ DESK ACCESSORY DONE ROUTINE ***********************
  102.  
  103. DADone
  104.     MOVEQ        #0,D0                            ; return no error
  105.     RTS                                         ; all done, exit
  106.  
  107.  
  108. ************************ DESK ACCESSORY CLOSE ROUTINE **********************
  109.  
  110. DAClose
  111.     MOVEM.L     A1-A4,-(SP)                     ; preserve A1-A4
  112.     MOVE.L        A1,A4                            ; MOVE DCE ptr to A4
  113.  
  114.     SUBQ.L        #4,SP                            ; FUNCTION = GrafPtr
  115.     MOVE.L        SP,-(SP)                        ; push a pointer to it
  116.     _GetPort                                    ; get it, now it's on TOS
  117.  
  118.     MOVE.L        DCtlEntry.DCtlWindow(A4),-(SP)    ; push the window
  119.     _DisposeWindow                                ; dispose of the window
  120.  
  121.     CLR.L        DCtlEntry.DCtlWindow(A4)        ; mark DCE properly
  122.     BRA.S        StdReturn                        ; all done with close, exit
  123.  
  124.  
  125. ********************** DESK ACCESSORY CONTROL ROUTINE **********************
  126.  
  127. DACtl
  128.     MOVE.L        A4,-(SP)                        ; preserve reg
  129.     MOVE.L        A1,A4                            ; move DCE ptr to A4
  130.     MOVE.W        CntrlParam.CSCode(A0),D0        ; get the control opCode
  131.     SUB.W        #accEvent,D0                    ; = 64? (event)
  132.     BEQ.S        DoCtlEvent
  133.     SUB.W        #1,D0                            ; = 65? (periodic)
  134.     BEQ.S        DoPeriodic
  135.  
  136. CtlDone
  137.     MOVE.L        A4,A1                            ; put DCE ptr back in A1
  138.     MOVE.L        (SP)+,A4                        ; restore reg
  139.     MOVEQ        #0,D0                            ; return no error
  140.     MOVE.L        JIODone,-(SP)                    ; jump to IODone
  141.     RTS
  142.  
  143.  
  144. ************************** EVENT HANDLING ROUTINE **************************
  145.  
  146. DoCtlEvent
  147.     MOVE.L        A3,-(SP)                        ; save reg
  148.     MOVE.L        CntrlParam.CSParam(A0),A3        ; get the event pointer
  149.     MOVE.W        EventRecord.what(A3),D0            ; get the event number
  150.     SUBQ        #updateEvt,D0                    ; is it an update?
  151.     BNE.S        CtlEvtDone                        ; If not, exit
  152.  
  153.     MOVE.L        EventRecord.message(A3),-(SP)    ; push windowPtr
  154.     _BeginUpdate                                ; begin the update operation
  155.     
  156.     MOVE.L        EventRecord.message(A3),-(SP)    ; push windowPtr again
  157.     _SetPort
  158.     BSR.S        DrawWindow                        ; draw our items
  159.         
  160.     MOVE.L        EventRecord.message(A3),-(SP)    ; one more time
  161.     _EndUpdate                                    ; end of update
  162.  
  163. CtlEvtDone
  164.     MOVE.L        (SP)+,A3                        ; restore reg
  165.     BRA.S        CtlDone                         ; exit
  166.  
  167.  
  168. **************************** PERIODIC ROUTINE *****************************
  169.  
  170. DoPeriodic
  171.     MOVE.L        DCtlEntry.DCtlWindow(A4),-(SP)    ; set the port
  172.     _SetPort
  173.  
  174.     BSR.S        DrawWindow                        ; draw our window every 5s
  175.     BRA.S        CtlDone
  176.  
  177.  
  178. ****************************** FONT METRICS *******************************
  179.  
  180. DrawWindow
  181.     MOVE.W        #SrcCopy,-(SP)                    ; source mode
  182.     _TextMode
  183.     MOVE.W        #Monaco,-(SP)                    ; Monaco
  184.     _TextFont
  185.     MOVE.W        #9,-(SP)                        ; 9 point
  186.     _TextSize
  187.     MOVE.W        #1,-(SP)                        ; bold
  188.     _TextFace
  189.  
  190. ********************** WRITE APPLICATION HEAP FREEMEM *********************
  191.  
  192.     MOVE.W        #6,-(SP)
  193.     MOVE.W        #10,-(SP)
  194.     _MoveTo
  195.     PEA         #'AppHeap: '
  196.     _DrawString
  197.     _FreeMem                                    ; free memory -> D0
  198.     JSR         PrintNum                        ; draw our free mem
  199.  
  200. ************************* WRITE SYSTEM HEAP FREEMEM ***********************
  201.  
  202.     PEA         #'  SysHeap: '
  203.     _DrawString
  204.     _FreeMem SYS                                ; free memory -> D0
  205.     JSR         PrintNum                        ; draw our free sys mem
  206.  
  207. ***************************** WRITELN VOL INFO ****************************
  208.  
  209.     PEA         #'  Disk: '
  210.     _DrawString
  211.  
  212.     MOVE.L        #HVolumeParam.sizeof,D0         ; size of HFS ParamBlock
  213.     _NewPtr CLEAR                                ; NewPtr -> A0
  214.     BNE.S        Exit                            ; IF Error THEN Exit
  215.     MOVE.L        A0,aPBPtr                        ; save PBPtr in D7
  216.     MOVE.L        DCtlEntry.DCtlWindow(A4),A1        ; get window rec pointer
  217.     LEA         aString(A1),A1                    ; address of string buffer
  218.     MOVE.L        A1,IOParam.ioNamePtr(A0)        ; ioNamePtr = Volume Name
  219.     _PBHGetVInfoSync                            ; _GetVolInfo    info -> A0^
  220.  
  221.     MOVE.L        aPBPtr,A0
  222.     MOVE.L        HVolumeParam.ioVAlBlkSiz(A0),D1    ; block size in D1
  223.     MOVE.W        HVolumeParam.ioVFrBlk(A0),D2     ; free blocks in D2
  224.     MOVE.W        D1,D0                            ; 32 bit * 16 bit multiply
  225.     MULU.W        D2,D0                            ; right half of size
  226.     SWAP        D1
  227.     MOVE.W        D1,D3
  228.     MULU.W        D2,D3                            ; left half of size
  229.     SWAP        D3
  230.     ADD.L        D3,D0                            ; total bytes free on vol
  231.     JSR         PrintNum                        ; write # bytes free
  232.  
  233.     PEA         #' free on '
  234.     _DrawString
  235.     MOVE.W        #4,-(SP)                        ; underlined
  236.     _TextFace
  237.     MOVE.L        aPBPtr,A0
  238.     MOVE.L        HVolumeParam.ioNamePtr(A0),-(SP)    ; offset for volName
  239.     _DrawString
  240.  
  241.     MOVE.L        aPBPtr,A0                        ; free the memory
  242.     _DisposePtr
  243.         
  244. Exit    
  245.     RTS
  246.  
  247. ***************************** SUBROUTINES ****************************
  248.  
  249. PrintNum
  250.  
  251.     ; Binary integer to be drawn at CurPenPos in D0 on entry
  252.     ; number drawn in plain text, bolding restored afterwords
  253.     
  254.     MOVE.L        D0,D6                            ; for safe keeping
  255.     CLR.W        -(SP)                            ; plain text
  256.     _TextFace
  257.     MOVE.L        D6,D0                            ; and back again
  258.     MOVE.L        DCtlEntry.DCtlWindow(A4),A0        ; get window rec pointer
  259.     LEA         aNumStr(A0),A0                    ; get buffer address
  260.     _NumToString                                ; Binary-Decimal Package
  261.     MOVE.L        A0,-(SP)                        ; push the pointer to the str
  262.     _DrawString
  263.     MOVE.W        #1,-(SP)                        ; bold text restored
  264.     _TextFace
  265.     RTS
  266.     
  267. ******************************* DATA AREA **********************************
  268.  
  269. theWindow    DC.W    322,10,338,500            ; window top,left,bottom,right
  270.  
  271.     ENDWITH
  272.     END
  273.  
  274.